package spr

import (
	pl "db2s/Or/spr/parser"
	"github.com/antlr4-go/antlr/v4"
)

func countFuncCall(node antlr.RuleNode, res *FunctionCallS) {
	for _, child := range node.GetChildren() {
		switch childNode := child.(type) {
		case *antlr.TerminalNodeImpl:
		//fmt.Println("------terminal-func=countFuncCall:", reflect.TypeOf(childNode), node.(antlr.RuleNode).GetText())
		case *pl.CountAllContext:
			res.Argument = append(res.Argument, ArgumentS{Value: childNode.GetText()})
		case *pl.DistinctKeywordContext:
			res.Argument = append(res.Argument, ArgumentS{Value: childNode.GetText()})
		case *pl.VariableContext:
			res.Argument = append(res.Argument, ArgumentS{Value: childNode.GetText()})
		default:
			countFuncCall(childNode.(antlr.RuleNode), res)
		}
	}
}

// 聚合函数
func argumentContext(node antlr.RuleNode, res *FunctionCallS) {
	for _, child := range node.GetChildren() {
		switch childNode := child.(type) {
		case *antlr.TerminalNodeImpl:
		case *pl.VariableContext:
			res.Argument = append(res.Argument, ArgumentS{Value: childNode.GetText()})
		default:
			//fmt.Println("-------default-func=argumentContext,", reflect.TypeOf(childNode), childNode.(antlr.RuleNode).GetText())
			argumentContext(childNode.(antlr.RuleNode), res)
		}
	}
}

func aggregateFuncCall(node antlr.RuleNode, res *FunctionCallS) {
	for _, child := range node.GetChildren() {
		switch childNode := child.(type) {
		case *antlr.TerminalNodeImpl:
			//fmt.Println("-----ppp:", reflect.TypeOf(node), childNode.GetText())
		case *pl.Over_clause_keywordContext:
			res.Name = childNode.GetText()
		case *pl.ArgumentContext:
			argumentContext(childNode, res)
		default:
			//fmt.Println("-------default-func=aggregateFuncCall,", reflect.TypeOf(node), reflect.TypeOf(childNode), childNode.(antlr.RuleNode).GetText())
			aggregateFuncCall(childNode.(antlr.RuleNode), res)
		}
	}
}
func functionCall(node antlr.RuleNode, res *FunctionCallS) {
	for _, child := range node.GetChildren() {
		switch childNode := child.(type) {
		case *antlr.TerminalNodeImpl:
			//fmt.Println("-----ppp:", reflect.TypeOf(node), childNode.GetText())
		case *pl.CountFunctionContext: //count函数
			//var countFunc = new(CountFuncCallS)
			res.Type = "count"
			res.Name = "count"
			countFuncCall(childNode, res)
			return
		case *pl.AggregateFunctionContext: //聚合函数和窗口函数
			//var aggregate = new(AggregateFuncCallS)
			res.Type = "aggregate"
			aggregateFuncCall(childNode, res)
			//res.Aggregate = *aggregate
		//case *pl.CastFunctionContext: //CAST 函数是 SQL 中用于数据类型转换的函数
		//case *pl.XmlCastFunctionContext: //XMLCAST 函数是 SQL 中用于将 XML 数据类型转换为其他数据类型的函数
		//case *pl.CollectFunctionContext: //COLLECT 函数是 SQL 中用于将多个值聚合成一个集合的函数
		//case *pl.ListaggFunctionContext: //LISTAGG 函数是 SQL 中用于将多个值连接成一个字符串的聚合函数
		//case *pl.WithInGroupFunctionContext: //结构通常与 SQL 中的窗口函数或聚合函数的使用相关，特别是在定义如何处理数据的顺序和分组时
		default:
			//fmt.Println("-------default-func=functionCall,", reflect.TypeOf(childNode), childNode.(antlr.RuleNode).GetText())
			functionCall(childNode.(antlr.RuleNode), res)
		}
	}
}
